﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.IO;
using SmartSpace.Core;
using SmartSpace.Packaging;

namespace SsTool.Install
{
    /// <summary>
    /// Interaction logic for PackPage.xaml
    /// </summary>
    public partial class PackPage : Page
    {
        private string _decompressFile;
        private string _compressFile;
        private string _compressXMML;

        public PackPage()
        {
            InitializeComponent();
        }

        private void _ctlBtnBroswerDecompressFile_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "zip文件(*.zip)|*.zip";
            dlg.Multiselect = false;
            if (dlg.ShowDialog() == true)
            {
                _decompressFile = dlg.FileName;
            }
        }

        private void _ctlBtnDecompress_Click(object sender, RoutedEventArgs e)
        {
            //FileInfo fi = new FileInfo(_decompressFile);
            //string zipFilePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fi.Name.Replace(fi.Extension,string.Empty));
            //PackageUtil.DecompressFile(_decompressFile, zipFilePath);

            //DirectoryInfo di = new DirectoryInfo(zipFilePath);
            //string file = di.GetFiles("*.sspack")[0].FullName;
            //Dictionary<string, string> configs = PackageUtil.ParsePackConfig(zipFilePath);
        }

        private void _ctlBtnBroswerCompressFile_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            //dlg.Filter = "主题配置文件(*.xtml)|*.xtml|模块配置文件(*.xmml)|*.xmml";
            dlg.Filter = "主题配置文件(*.xtml)|*.xtml";
            dlg.Multiselect = false;
            if (dlg.ShowDialog() == true)
            {
                _compressFile = dlg.FileName;
            }
        }

        private void _ctlBtnCompress_Click(object sender, RoutedEventArgs e)
        {
            Theme theme;
            Dictionary<string, string> configs = PackageUtil.ParseXTML(_compressFile, out theme);
            FileInfo fi = new FileInfo(_compressFile);
            string folder = fi.Directory.FullName;
            List<string> files = new List<string>();
            files.AddRange(configs.Keys);
            files.Add(fi.Name);

            string zipFilePath = string.Format("{0}\\{1}{2}.sstp", fi.Directory.FullName, fi.Name.Replace(fi.Extension, string.Empty), theme.Version);
            PackageUtil.CompressFiles(folder, files, zipFilePath);
            MessageBox.Show("打包完毕!\r\n" + zipFilePath);
        }

        private void _ctlBtnBroswerXMML_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "模块部署配置文件(*.xmml)|*.xmml";
            dlg.Multiselect = false;
            if (dlg.ShowDialog() == true)
            {
                _compressXMML = dlg.FileName;
            }
        }

        private void _ctlBtnCompressXMML_Click(object sender, RoutedEventArgs e)
        {
            Module module;
            Dictionary<string, string> configs = PackageUtil.ParseXMML(_compressXMML, out module);
            FileInfo fi = new FileInfo(_compressXMML);
            string folder = fi.Directory.FullName;
            List<string> files = new List<string>();
            files.AddRange(configs.Keys);
            files.Add(fi.Name);

            string zipFilePath = string.Format("{0}\\{1}{2}.ssmp", fi.Directory.FullName, fi.Name.Replace(fi.Extension, string.Empty), module.Version);

            PackageUtil.CompressFiles(folder, files, zipFilePath);
            MessageBox.Show("打包完毕!\r\n" + zipFilePath);
        }
    }
}
